﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Nop.Plugin.Nova.Janrain.Service
{
    public class JanrainEngageUser
    {
        public JanrainEngageUser(XmlElement Document)
        {
            Init(Document);
        }
        public JanrainEngageUser() { }

        private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        private string email;
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        private string identifier;
        public string Identifier
        {
            get { return identifier; }
            set { identifier = value; }
        }

        private string userName;
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private string provider;
        public string Provider
        {
            get { return provider; }
            set { provider = value; }
        }

        private string photo;
        public string Photo
        {
            get { return photo; }
            set { photo = value; }
        }

        private string providerAccessToken;
        public string ProviderAccessToken
        {
            get { return providerAccessToken; }
            set { providerAccessToken = value; }
        }

        private string gender;
        public string Gender
        {
            get { return gender; }
            set { gender = value; }
        }

        private string birthday;
        public string Birthday
        {
            get { return birthday; }
            set { birthday = value; }
        }

        //for Facebook
        private string facebookId;
        public string FacebookId
        {
            get
            {
                return facebookId;
            }
            set
            {
                facebookId = value;
            }
        }

        private void Init(XmlElement element)
        {
            this.Provider = element.SelectSingleNode("/rsp/profile/providerName").NotNullInnerText();
            this.UserName = element.SelectSingleNode("/rsp/profile/preferredUsername").NotNullInnerText();
            this.Identifier = element.SelectSingleNode("/rsp/profile/identifier").NotNullInnerText();
            this.Email = element.SelectSingleNode("/rsp/profile/email").NotNullInnerText();
            this.Photo = element.SelectSingleNode("/rsp/profile/photo").NotNullInnerText();

            if (this.Provider == "Google")
            {
                this.FirstName = element.SelectSingleNode("/rsp/profile/name/givenName").NotNullInnerText();
                this.LastName = element.SelectSingleNode("/rsp/profile/name/familyName").NotNullInnerText();
            }
            else if (this.Provider == "Yahoo!")
            {
                //Need to split up the "formatted" name to give us FirstName and LastName values
                string[] temp = element.SelectSingleNode("/rsp/profile/name/formatted").NotNullInnerText().Split(' ');
                this.Email = element.SelectSingleNode("/rsp/profile/email").NotNullInnerText();
                this.FirstName = temp[0];
                if (temp.Length > 0)
                {
                    this.LastName = temp[1];
                }
                //this.Photo = Element.SelectSingleNode("/rsp/profile/photo").InnerText;
            }
            else if (this.Provider == "Facebook")
            {
                this.Email = element.SelectSingleNode("/rsp/profile/email").NotNullInnerText();
                this.FirstName = element.SelectSingleNode("/rsp/profile/name/givenName").NotNullInnerText();
                this.LastName = element.SelectSingleNode("/rsp/profile/name/familyName").NotNullInnerText();
                this.Photo = element.SelectSingleNode("/rsp/profile/photo").NotNullInnerText();
                this.ProviderAccessToken = element.SelectSingleNode("/rsp/profile/accessCredentials/accessToken").NotNullInnerText();
                this.FacebookId = element.SelectSingleNode("/rsp/profile/identifier").NotNullInnerText();
            }
            else if (this.Provider == "Twitter")
            {
                //Need to split up the "formatted" name to give us FirstName and LastName values
                string[] temp = element.SelectSingleNode("/rsp/profile/name/formatted").NotNullInnerText().Split(' ');
                this.FirstName = temp[0];
                if (temp.Length > 0)
                {
                    this.LastName = temp[1];
                }
                this.Email = element.SelectSingleNode("/rsp/profile/email").NotNullInnerText();
                this.ProviderAccessToken = element.SelectSingleNode("/rsp/profile/accessCredentials/oauthToken").NotNullInnerText();
            }
            else if (this.Provider == "LinkedIn")
            {
                //Need to split up the "formatted" name to give us FirstName and LastName values
                string[] temp = element.SelectSingleNode("/rsp/profile/name/formatted").NotNullInnerText().Split(' ');
                this.FirstName = temp[0];
                if (temp.Length > 0)
                {
                    this.LastName = temp[1];
                }
                this.Email = element.SelectSingleNode("/rsp/profile/email").NotNullInnerText();
            }
            else if (this.provider == "Windows Live")
            {
                //Need too split up the "formatted" name to give us Firstname and LastName values
                string[] temp = element.SelectSingleNode("/rsp/profile/name/formatted").NotNullInnerText().Split(' ');
                this.FirstName = temp[0];
                if (temp.Length > 0)
                {
                    this.LastName = temp[1];
                }
                this.Email = element.SelectSingleNode("/rsp/profile/email").NotNullInnerText();
                this.Gender = element.SelectSingleNode("/rsp/profile/gender").NotNullInnerText();
                this.Birthday = element.SelectSingleNode("/rsp/profile/birthday").NotNullInnerText();
            }
        }
    }
}
